﻿// ***********************************************************************************************
// <summary author="bill.qian" create_date="2009/9/8" modify_date="">
//         SharePoint工具类
// </summary> 
// <modify_log>
//
// </modify_log>
// <copyright file="SPHelper.cs" company="DEXTRYS" url="http://www.dextrys.com">
//           新宇软件（苏州工业园区）有限公司
// </copyright>
// ***********************************************************************************************
namespace Dextrys.SharePoint.Util
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using Microsoft.SharePoint.Workflow;
    using Microsoft.SharePoint.WorkflowUtil;

    /// <summary>
    /// SharePoint Helper Functions
    /// </summary>
    /// <author> Bill Qian </author>
    public static class SPHelper
    {
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="web">上下文web</param>
        /// <param name="to">发给谁</param>
        /// <param name="title">邮件标题</param>
        /// <param name="body">邮件内容</param>
        public static void SendMail(SPWeb web, string to, string title, string body)
        {
            SPUtility.SendEmail(web, true, true, to, title, body);
        }

        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="web">下文web</param>
        /// <param name="user">发给谁</param>
        /// <param name="title">邮件标题</param>
        /// <param name="body">邮件内容</param>
        public static void SendMail(SPWeb web, SPUser user, string title, string body)
        {
            SendMail(web, user.Email, title, body);
        }

        /// <summary>
        /// 获取ID
        /// </summary>
        /// <param name="input">输入的Lookup属性中的字符串值</param>
        /// <returns>列表项目ID</returns>
        /// <remarks>
        /// 在SharePoint列表字段中，获取的Lookup属性往往是"ID;显示字段"这样的形式，本方法获取前面的ID，如果失败则返回-1
        /// </remarks>
        public static int GetId(string input)
        {
            int start = input.IndexOf(";");
            return start > -1 ? Convert.ToInt32(input.Substring(0, start)) : -1;
        }

        /// <summary>
        /// 在指定的ListItem上启动一个工作流，如果工作流不存在，则不作任何工作
        /// </summary>
        /// <param name="person">用来启动工作流的用户</param>
        /// <param name="listItem">对应的ListItem</param>
        /// <param name="flowName">工作流名称</param>
        public static void StartWorkflow(SPUser person, SPListItem listItem, string flowName)
        {
            SPWorkflowManager workflowManager = listItem.Web.Site.WorkflowManager;
            SPWorkflowAssociationCollection workflowAssoList = listItem.ParentList.WorkflowAssociations;

            foreach (SPWorkflowAssociation asso in workflowAssoList)
            {
                if (asso.Name.ToLower() == flowName.ToLower())
                {
                    string data = asso.AssociationData;
                    if (person != null)
                    {
                        string destXml = "<my:CC><my:Person><my:DisplayName>" + person.Name + "</my:DisplayName><my:AccountId>" +
                            person.LoginName + "</my:AccountId><my:AccountType>User</my:AccountType></my:Person></my:CC>";
                        data.Replace("<my:CC></my:CC>", destXml);
                    }

                    // here we go
                    SPWorkflow wf = workflowManager.StartWorkflow(listItem, asso, data);
                }
            }
        }
    }
}
